home *** CD-ROM | disk | FTP | other *** search
-
-
-
- QSORT(3) MINTLIB LIBRARY FUNCTIONS QSORT(3)
-
-
- N✓NA✓AM✓ME✓E
- qsort - quicker sort
-
- S✓SY✓YN✓NO✓OP✓PS✓SI✓IS✓S
- #include <stdlib.h>
-
- qsort(void *base, size_t total_elems, size_t elem_size,
- int (*compare)(const void *one, const void *two));
-
- D✓DE✓ES✓SC✓CR✓RI✓IP✓PT✓TI✓IO✓ON✓N
- qsort is an implementation of the quicker-sort algorithm.
- It sorts a table of data in place.
- - base points to the element at the base of
- the table.
- - total_elems is the number of elements in
- the table.
- - elem_size is the size, in bytes, of each element
- in the table.
- - compare is the name of the comparison function,
- which is called with two arguments that point to
- the elements being compared. As the function must
- return an integer less than, equal to, or greater
- than zero, so must the first argument to be considered
- be less than, equal to, or greater than the second.
-
- E✓EX✓XA✓AM✓MP✓PL✓LE✓E
- The following program sorts a simple array:
- static int intcompare(const void *i, const void *j)
- {
- int *one, *two;
-
- one = (int *)i;
- two = (int *)j;
- return (*one - *two);
- }
-
- void main(void)
- {
- int a[10];
- int i;
-
- a[0] = 9;
- a[1] = 8;
- a[2] = 7;
- a[3] = 6;
- a[4] = 5;
- a[5] = 4;
- a[6] = 3;
- a[7] = 2;
- a[8] = 1;
- a[9] = 0;
-
- qsort(a, 10, sizeof(int), intcompare);
- for (i = 0; i < 10; i++)
-
-
-
- MiNT docs 0.1 3 March 1993 1
-
-
-
-
-
- QSORT(3) MINTLIB LIBRARY FUNCTIONS QSORT(3)
-
-
- printf(" %d", a[i]);
- printf("n");
- }
-
- S✓SE✓EE✓E A✓AL✓LS✓SO✓O
- b✓bs✓se✓ea✓ar✓rc✓ch✓h(✓(3✓3)✓)
-
- N✓NO✓OT✓TE✓ES✓S
- The comparison function need not compare every byte, so
- arbitrary data may be contained in the elements in addi-
- tion to the values being compared.
-
- The order in the output of two items which compare as
- equal is unpredictable.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- MiNT docs 0.1 3 March 1993 2
-
-
-